//
//  ZLoadView.swift
//  swift-weibo
//
//  Created by zero on 17/3/20.
//  Copyright © 2017年 zero. All rights reserved.
//

import UIKit

import SnapKit

@objc protocol LoadViewDelegate {
    
    @objc optional func registClick(load:ZLoadView)
    
    func loginClick(load:ZLoadView)
}

class ZLoadView: UIView {

    weak var delegate:LoadViewDelegate?
    

    //  重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        addBaseAnimation()
    }
    
    //
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setupUI()
        addBaseAnimation()
    }
    
    //  切换视图
    func updateView(imageName:String,title:String) {
        
        cicrleView.image = UIImage(named: imageName)
        titleLabel.text = title
        
        coverView.isHidden = true
        hourseView.isHidden = true
        
        cicrleView.layer.removeAllAnimations()
        
    }
    
    private func addBaseAnimation() {
        
        let baseAnimation = CABasicAnimation(keyPath: "transform.rotation")
        
        baseAnimation.toValue = 2 * M_PI
        baseAnimation.duration = 15
        baseAnimation.repeatCount = MAXFLOAT
        baseAnimation.isRemovedOnCompletion = false
        
        cicrleView.layer.add(baseAnimation, forKey: nil)
    }
    
    private func setupUI(){
        
        //  圈圈
        addSubview(cicrleView)
        
        cicrleView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
            
        }
        
        //  遮罩视图
        addSubview(coverView)
        
        coverView.snp.makeConstraints({ (make) in
            
            make.center.equalTo(cicrleView)
        })
        
        //  房子
        addSubview(hourseView)
        
        hourseView.snp.makeConstraints({ (make) in
            
            make.center.equalTo(cicrleView)
        })
        
        //  标题
        addSubview(titleLabel)
        
        titleLabel.preferredMaxLayoutWidth = 200
        titleLabel.numberOfLines = 0
        
        titleLabel.snp.makeConstraints { (make) in
            
            make.leading.equalTo(cicrleView)
            make.top.equalTo(cicrleView.snp.bottom).offset(8)
        }
        
        //  注册
        addSubview(registBtn)
        
        registBtn.snp.makeConstraints { (make) in
            
            make.leading.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.width.equalTo(80)
            make.height.equalTo(35)
        }
        
        //  登录
        addSubview(loginBtn)
        
        loginBtn.snp.makeConstraints { (make) in
            
            make.trailing.equalTo(titleLabel)
            make.top.equalTo(registBtn)
            make.size.equalTo(registBtn)
        }
        
        //  添加点击事件
        registBtn.addTarget(self, action: #selector(registBtnClick), for: .touchUpInside)
        
        loginBtn.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        
    }

    //  点击事件
    @objc private func registBtnClick(){
        
        delegate?.registClick!(load: self)
    }
    
    @objc private func loginBtnClick(){
        
        delegate?.loginClick(load: self)
    }
    
    //  懒加载空间
    
    private lazy var cicrleView:UIImageView = {
       
    
        var imageView = UIImageView()
        
        imageView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return imageView
        
    }()
    
    private lazy var coverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var hourseView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    private lazy var titleLabel:UILabel = UILabel(title: "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈", fontColor: UIColor.black , fontSize: 14)
    
    private lazy var registBtn:UIButton = UIButton(title: "注册", fontColor: UIColor.orange, fontSize: 14, imageName: nil, backImage: "common_button_white")
 
    private lazy var loginBtn:UIButton = UIButton(title: "登录", fontColor: UIColor.black, fontSize: 14, imageName: nil, backImage: "common_button_white")
}
